import { Component, OnInit } from '@angular/core';
import { LocalStorage } from '@core/local.storage';
import { ApiService } from '../../../../core/api.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { forkJoin } from 'rxjs';

@Component({
    selector: 'app-gjzl',
    templateUrl: './gjzl.component.html',
    styleUrls: ['./gjzl.component.less']
})

export class GjzlComponent implements OnInit {
    userInfo = JSON.parse(this.ls.get('loginInfo'));
    dateFormat = 'yyyy/MM/dd HH:mm:ss';
    lockHeadScroll = { y: window.innerHeight - 350 + 'px' };
    isMainPage = true;
    lastNode: any;
    saveStatus = [];
    showAdd = false;
    data = {
        tableData: [],
        tableTotal: 0,
        tablePagination: {
            page: 1,
            rows: 10,
        },
        addInfo: {
            equistylename: '',
            equistylecode: '',
        },
        ctrlNames: {
            bm: 'bm',
            fzr: 'fzr',
            sjStart: 'sjStart',
            sjEnd: 'sjEnd',
        },
        nodes: [
        ],
        bjtj: { allalert: 0, ckalert: 0 },
    };



    constructor(public ls: LocalStorage,
        private apiService: ApiService,
        private message: NzMessageService
    ) { }

    ngOnInit() {
        this.getBm();
        this.getBjTj();
        this.query();
    }

    stringEqual(obj1: object, obj2: object) {
        const str1 = JSON.stringify(obj1);
        const str2 = JSON.stringify(obj2);
        return str1 == str2;
    }

    getBm() {
        this.apiService.gjqdgl_dispatcher(
            'sxdgl_bm', {}, 'query', 'post'
        ).subscribe(res => {
            let _data;
            if (typeof res == 'string') {
                _data = JSON.parse(res);
            } else {
                _data = res;
            }
            this.data.nodes = _data.filter(el => el.children);
            this.data.nodes.unshift({
                title: '全部',
                // key: '',
                // children:[]
            });
        });
    }

    /**
     *
           taskname	任务名称
            shopName	车间
            areaName	工区
            toolsamountwrongreason	遗失清单
            name 	工具检查负责人
            beforeworkscandatetime	工具下道时间
            afterworkscandatetime	工具上道时间
            remark	备注
     */
    query(reset: boolean = false) {
        if (reset == true || this.data.tablePagination.page == 0) {
            this.data.tablePagination.page = 1;
        }
        // let vfv = this.validateForm.value;
        const ctrlNames = this.data.ctrlNames;
        const param: any = {};

        param.page = this.data.tablePagination.page;
        param.rows = this.data.tablePagination.rows;
        this.apiService.gjqdgl_dispatcher(
            'gjzl_table', param, 'query', 'post'
        ).subscribe(res => {
            let _data;
            if (typeof res == 'string') {
                _data = JSON.parse(res);
            } else {
                _data = res;
            }
            this.data.tableTotal = _data.totalCount;
            this.data.tableData = _data.data;
            this.saveStatus = new Array(_data.data.length);
            this.saveStatus.fill(false, 0, _data.data.length);
        });
    }

    getBjTj() {
        this.apiService.gjqdgl_dispatcher(
            'gjqdgl_bjtj', {}, 'query', 'post'
        ).subscribe(res => {
            let _data;
            if (typeof res == 'string') {
                _data = JSON.parse(res);
            } else {
                _data = res;
            }
            this.data.bjtj = _data.data;
        });
    }

    nzTreeClick($e: any): void {
        this.lastNode = $e.node;
    }

    nzPageIndexChange() {
        if (this.data.tablePagination.page == 0) {
            return;
        }
        this.query();
    }

    setPageSize() {

        this.query(true);

    }

    getObs(taskids) {
        const obs = [];
        taskids.forEach(el => {
            const ob = this.apiService.gjqdgl_dispatcher(
                'gjlb_sh', { taskId: el }, 'save', 'post'
            );
            obs.push(ob);
        });
        return obs;
    }

    showAddBar() {
        this.showAdd = !this.showAdd;

    }
    showSave(i) {
        this.saveStatus[i] = !this.saveStatus[i];
    }

    save(optType: string, index?) {
        let param: any = {};
        if (optType == 'add') {

            param.equistylename = this.data.addInfo.equistylename;
            param.equistylecode = this.data.addInfo.equistylecode;
        } else if (optType == 'save') {
            const row = this.data.tableData[index];
            if (row.equistyleid) {
                param.equistyleid = row.equistyleid;
            } else {
                return;
            }
            param = row;
        }

        this.apiService.gjqdgl_dispatcher(
            'gjzl_table', param, optType, 'post'
        ).subscribe(res => {
            let _data;
            if (typeof res == 'string') {
                _data = JSON.parse(res);
            } else {
                _data = res;
            }
            if (_data.code == 'success') {
                this.message.create('success', '保存成功');
            } else {
                this.message.create('error', '保存失败');
            }
            if (optType == 'add') {
                this.data.addInfo.equistylename = '';
                this.data.addInfo.equistylecode = '';
                this.showAdd = false;
            }
            this.query(false);
        });
    }



}
